package cn.agiledata.bank.linkbank.impl.validate;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import cn.agiledata.bank.linkbank.exception.LinkBankException;

/**
 * 数据域枚举取值校验器
 * 
 * @author maxianbo
 * 
 */
public final class FieldEnumValidator extends AbstractFieldValidator {
    private Set enums = new HashSet();

    public void setEnums(Set enums) {
        this.enums = enums;
    }

    public void validate(String value) throws LinkBankException {
        if (value == null) {
            validateNullable(value);
        } else if ("".equals(value)) {
            validateEmptiable(value);
        } else {
            String enumsDesc = "";
            for (Iterator it = this.enums.iterator(); it.hasNext();) {
                String e = (String) it.next();
                if (e.equals(value)) {
                    return;
                }
                enumsDesc += e + ", ";
            }
            if (enumsDesc.length() >= 2) {
                enumsDesc = enumsDesc.substring(0, enumsDesc.length() - 2);
            }
            throw LinkBankException.getEnumInvalidException(getTarget(),
                    enumsDesc, getErrorCode());
        }
    }

}
